package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.MoreDataQueryListAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.KnowledgeModel;
import com.banshuibao.bsb_android.model.RequestMoreModel;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshBase;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshListView;
import com.banshuibao.bsb_android.util.IntentManager;

import java.util.ArrayList;
import java.util.List;

public class MoreDataQueryActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener2{

    private PullToRefreshListView pullToRefreshListView;
    private String content,title,tag,type;
    private ListView listView;
    private int statrId,endId;
    private int rows;
    private boolean isUp;
    private MoreDataQueryListAdapter adapter;
    private List<String> list;
    //flag: 1知识库传来  2案例库传来
    private int flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_data_query);
        content = getIntent().getStringExtra("content");
        title = getIntent().getStringExtra("title");
        tag = getIntent().getStringExtra("tag");
        type = getIntent().getStringExtra("type");
        list = new ArrayList<>();
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        switch (tag) {
            case "repository":
                flag = 1;
                repositoryDefault();
                break;
            case "case":
                flag = 2;
                caseDefault();
                break;
        }
    }

    public void initView(){
        setTitleText(title);
        pullToRefreshListView = findViewById(R.id.ptrlv);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(this);
        listView = pullToRefreshListView.getRefreshableView();
        adapter = new MoreDataQueryListAdapter(this);
        listView.setAdapter(adapter);
        listView.setDivider(null);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                KnowledgeModel.DataBean dataBean = adapter.getItem(position-1);
                IntentManager.goKnowledgeDetailsAcitivty(MoreDataQueryActivity.this,dataBean.getUuid(),flag);
            }
        });
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        switch (tag) {
            case "repository":
                repositoryDefault();
                break;
            case "case":
                caseDefault();
                break;
        }
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        isUp = true;
        endId = statrId+9;
        switch (tag) {
            case "repository":
                repositoryNet();
                break;
            case "case":
                requestCaseNet();
                break;
        }
    }

    //知识库
    public void repositoryNet() {
        RequestMoreModel moreModel = new RequestMoreModel();
        moreModel.setSinceId(statrId);
        moreModel.setMaxId(endId);
        moreModel.setContent(content);
        moreModel.setType(type);
        HttpUtil.doPost(Constants.Url.KNOWLEDGE_SEARCH_MORE, moreModel, new HttpResponse(MoreDataQueryActivity.this, KnowledgeModel.class) {
            @Override
            public void onResult(Object obj) {
                KnowledgeModel model = (KnowledgeModel) obj;
                if(model!=null){
                    List<KnowledgeModel.DataBean> data = model.getData();
                    rows = model.getRows();
                    statrId += rows;
                   switch (type){
                       case "0":
                           if(isUp){
                               adapter.setNavigationUp(data);
                           }else{
                               adapter.setNavigationDown(data);
                           }

                           break;
                       case "1":
                           if(isUp){
                               adapter.setCollectionUp(data);
                           }else{
                               adapter.setCollectionDown(data);
                           }

                           break;
                   }
                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                pullToRefreshListView.onRefreshComplete();
            }
        });

    }

    //案例库
    public void requestCaseNet(){
        RequestMoreModel moreModel = new RequestMoreModel();
        moreModel.setSinceId(statrId);
        moreModel.setMaxId(endId);
        moreModel.setContent(content);
        moreModel.setType(type);
        HttpUtil.doPost(Constants.Url.CASE_SEARCH_MORE, moreModel, new HttpResponse(MoreDataQueryActivity.this, KnowledgeModel.class) {
            @Override
            public void onResult(Object obj) {
                KnowledgeModel model = (KnowledgeModel) obj;
                if(model!=null){
                    List<KnowledgeModel.DataBean> data = model.getData();
                    rows = model.getRows();
                    statrId += rows;
                    switch (type){
                        case "0":
                            if(isUp){
                                adapter.setTaxUp(data);
                            }else{
                                adapter.setTaxDown(data);
                            }

                            break;
                        case "1":
                            if(isUp){
                                adapter.setJudicialUp(data);
                            }else{
                                adapter.setJudicialDown(data);
                            }

                            break;
                        case "2":
                            if(isUp){
                                adapter.setAuditingUp(data);
                            }else{
                                adapter.setAuditingDown(data);
                            }
                            break;
                    }
                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                pullToRefreshListView.onRefreshComplete();
            }
        });
    }

    public void repositoryDefault(){
        statrId = 1;
        endId = statrId + 9;
        if(adapter!=null&&adapter.getCount()>10){
            endId = adapter.getCount();
        }
        isUp = false;
        repositoryNet();
    }

    public void caseDefault(){
        statrId = 1;
        endId = statrId + 9;
        isUp = false;
        if(adapter!=null&&adapter.getCount()>10){
            endId = adapter.getCount();
        }
        requestCaseNet();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        statrId = 0;
        endId = statrId + 9;
        rows = 0;
        if(adapter!=null){
            adapter.clearAdapter();
        }
    }

    @Override
    public void finish() {
        super.finish();
        statrId = 0;
        endId = statrId + 9;
        rows = 0;
        if(adapter!=null){
            adapter.clearAdapter();
        }
    }
}
